package com.wthink.newMain.view.changeLine

import android.app.Activity
import android.app.Dialog
import android.os.Bundle
import android.view.*
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.GridLayoutManager
import com.wthink.card.models.GMBInfoBundle
import com.wthink.hzgj.BR
import com.wthink.hzgj.R
import com.wthink.hzgj.databinding.DialogChangeLineBinding
import com.wthink.hzgj.home.data.Line
import com.wthink.hzgj.util.`interface`.LineCallBack
import com.wthink.hzgj.util.`interface`.SchedleRefuseCallBack
import com.wthink.hzgj.util.adapter.BaseViewHolder
import com.wthink.hzgj.util.adapter.OnItemClickListener
import kotlinx.android.synthetic.main.dialog_change_line.back
import kotlinx.android.synthetic.main.dialog_change_line.commit
import kotlinx.android.synthetic.main.item_change_line_dialog.view.*

class ChangeLineDialogX(val context: Activity) : Dialog(context, R.style.dialog) {
    private lateinit var binding: DialogChangeLineBinding
    var lineNum: String = ""
    var lineId: String = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.inflate<DialogChangeLineBinding>(
            LayoutInflater.from(context),
            R.layout.dialog_change_line, null, false
        )
        setContentView(binding.root)
        binding.setVariable(BR.dialog, this)
        binding.executePendingBindings()
        // start()
    }

    fun setList(lineName: String, list: List<Line>?) {
        fixList.postValue(list)

        var lastPosition = GMBInfoBundle.getLastPosition()

        binding.dialogLineRecycle.layoutManager = GridLayoutManager(context, 3)

        binding.dialogLineRecycle.adapter = object : ChangeLineDialogAdapter() {
            override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
                super.onBindViewHolder(holder, position)

/*
                if (lineName.isNotEmpty()) {
                    if (lineName == holder.itemView.tv1.text) {
                        GMBInfoBundle.setLastPosition(position)
                    }
                }
*/
                holder.itemView.iv1.isSelected = position == lastPosition
                if (holder.itemView.iv1.isSelected) {
                    lineNum = holder.itemView.tv1.text.toString()
                    lineId = list!![position].id
                }

                holder.itemView.setOnClickListener {
                    val position1 = lastPosition
                    GMBInfoBundle.setLastPosition(position)
                    lastPosition = position
                    notifyItemChanged(position)
                    notifyItemChanged(position1)
                    lineNum = holder.itemView.tv1.text.toString()
                    lineId = list!![position].id
                }
            }
        }.apply {
            if (list != null) {
                update(list)
            }
            mItemClickListener = object : OnItemClickListener {
                override fun onItemClick(obj: Any, position: Int) {
                    //chosen?.isSelected = true
                }
            }
        }
    }

    val fixList = MutableLiveData(emptyList<Line>())
    fun start() {
        var array: Array<String> = arrayOf(
            "M361", "M362", "M363", "M364", "M365", "M366", "M367", "M368", "M369"
        )
        val arrayList = ArrayList<String>()
        for (item in array) {
            arrayList.add(item)
        }
        //fixList.postValue(arrayList)
    }

    override fun show() {
        super.show()
        window?.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        window?.setGravity(Gravity.CENTER)

        back.setOnClickListener {
            dismiss()
        }

        commit.setOnClickListener {
            dismiss()
            lineCallBack.callBack(lineNum,lineId)
        }
    }

    fun setOnclickListener(listener: View.OnClickListener) {
        if (back !== null) {
            back.setOnClickListener(listener)
            back.setOnClickListener {
                dismiss()
            }
        }
        if (commit !== null) {
            commit.setOnClickListener(listener)
        }
    }

    lateinit var lineCallBack: LineCallBack

    fun setLineCall(callBack: LineCallBack) {
        lineCallBack = callBack
    }
}